package com.example.arithmeticleetcode.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: arithmetic-leetcode
 * @description: 5388. 重新格式化字符串
 * @author: FangZhen
 * @create: 2020-04-20 16:12
 **/
public class Demo04 {

    public static void main(String[] args) {
        String str = "az";
        char[] chars = str.toCharArray();
        for (char aChar : chars) {
            System.out.println(aChar);
        }
        char charFirstValue = 'a';
        char charEndValue = 'z';
        System.out.println(charFirstValue < charEndValue);
        System.out.println((byte) charEndValue);
        System.out.println((byte) charEndValue);
        StringBuffer charSb = new StringBuffer();
        System.out.println(charSb.toString().length());
        System.out.println("======================================================");
        System.out.println(reformat("ab123"));
    }

    public static String reformat(String s) {
        if (s == null && s.length() == 0) {
            return "";
        }
        StringBuffer charSb = new StringBuffer();
        StringBuffer numberSb = new StringBuffer();
        for (int i = 0, len = s.length(); i < len; i++) {
            char c = s.charAt(i);
            if ('a' <= c && c <= 'z') {
                charSb.append(c);
            }
            if (48 <= (byte) c && 57 >= (byte) c) {
                numberSb.append(c);
            }
        }
        StringBuffer sb = new StringBuffer();
        if ((charSb.length() - numberSb.length()) >= -1 && (charSb.length() - numberSb.length()) <= 1) {
            String charStr = charSb.toString();
            String numberStr = numberSb.toString();
            boolean f = charStr.length() > numberStr.length();
            int len = f ? numberStr.length() : charStr.length();
            for (int i = 0; i < len; i++) {
                if (f) {
                    sb.append(charStr.charAt(i)).append(numberSb.charAt(i));
                } else {
                    sb.append(numberSb.charAt(i)).append(charStr.charAt(i));
                }
            }
            if (f) {
                sb.append(charStr.charAt(len));
            } else {
                if (charStr.length() != numberStr.length()) {
                    sb.append(numberStr.charAt(len));
                }
            }
        }
        return sb.toString();

    }
}
